package com.gba.mark;

import com.gba.mark.article.entity.Article;
import com.gba.mark.article.service.IArticleService;
import com.gba.mark.origin.entity.Origin;
import com.gba.mark.origin.entity.OriginStatus;
import com.gba.mark.origin.service.IOriginService;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UploadArticle {

    @Resource
    private IOriginService originServiceImpl;
    @Resource
    private IArticleService articleServiceImpl;

    @Test
    public void test3() throws IOException {
        File xlsFile = new File("D:\\语料来源文件\\(已导)香港特别行政区政府新闻公报(20.2.1-20.2.25).xls");
        // 获得工作簿对象
        Workbook workbook = null;
        try {
            workbook = Workbook.getWorkbook(xlsFile);
        } catch (BiffException e) {
            e.printStackTrace();
        }
        // 获得所有工作表
        Sheet[] sheets = workbook.getSheets();
        // 遍历工作表
        if (sheets != null)
        {
            for (Sheet sheet : sheets)
            {
                // 获得行数
                int rows = sheet.getRows();
                // 读取数据并存储
                for (int row = 1; row < rows; row++)
                {
                    //xls文件规定
                    //第一列为文章标题
                    String title = sheet.getCell(0, row).getContents();
                    //第二列为链接地址
                    String url = sheet.getCell(1, row).getContents();
                    //第三列为正文内容
                    String text = sheet.getCell(2, row).getContents();
                    //第四列为日期信息
                    String date = sheet.getCell(3, row).getContents();
                    //第五列为文章来源
                    String source = sheet.getCell(4, row).getContents();
                    //第六列为语言信息
                    String language = sheet.getCell(5, row).getContents();
                    text = text.replaceAll("＊","")
                            .replaceAll("　","")
                            .replaceAll(" ","")
                            .replaceAll(" ","")
                            .replaceAll("\t","")
                            .replaceAll("\n","");
                    date = date.replaceAll("星期一","")
                            .replaceAll("星期二","")
                            .replaceAll("星期三","")
                            .replaceAll("星期四","")
                            .replaceAll("星期五","")
                            .replaceAll("星期六","")
                            .replaceAll("星期日","")
                            .replaceAll("日","")
                            .replaceAll("（香港时间）","")
                            .replaceAll("年","-").replaceAll("月","-").replaceAll("时",":").replaceAll("分",":00");
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    if(isChinnese(text)){
                        //获取当前时间
                        Date date1 = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String date2 = sdf.format(date1);

                        Article article = new Article();
                        article.setContent(text);
                        article.setTitle(title);
                        article.setUrl(url);
                        article.setGroupId(14);
                        article.setSourceId(2);


                        //
//                        article.setSource(source);
                        if (!date.isEmpty()) {
                            try {
                                //parse将时间由String类型转为int类型
                                article.setDate(format.parse(date));
                                article.setPostDate(sdf.parse(date2));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }
                        articleServiceImpl.save(article);

                        String regEx="[。？！?!]";
                        Pattern p =Pattern.compile(regEx);
                        /*按照句子结束符分割句子*/
                        String[] segments = p.split(text);
                        for(int location = 0 ; location<segments.length;location++){
                            String segment = segments[location];
                            Origin origin = new Origin();
                            origin.setSentence(segment);
                            origin.setSystemLabel(NLPTokenizer.analyze(segment).toString());
//                            origin.setSource(source);
//                            origin.setLanguage(language);
                            origin.setStatus(OriginStatus.UNLABELED.getData());
                            origin.setArticleId(article.getId());
                            origin.setLocation(location);
                            origin.setUpdateDate(new Date());
                            origin.setOptLock(0);
                            origin.setActive(true);
                            System.out.println(origin);
                            originServiceImpl.save(origin);

                            }
                        }
                    }
                }
            }workbook.close();
        }


    private static boolean isChinnese(String str) {
        String reg = "[\\u4E00-\\u9FA5]+";
        Matcher m = Pattern.compile(reg).matcher(str);
        if (m.find()) {
            return true;
        }else{
            return false;
        }
    }


}